//===============================
//  功能 ：备注 
//  作者 ：上中野辅亚瑟王 
//  创建时间 ：2024-11-28  18-55-08 
//  Unity版本  ：2019.4.35f1 
//  变更时间 :  2024-11-28  18-55-08 
//===============================





//===============================
//  功能 ：备注 
//  作者 ：上中野辅亚瑟王 
//  创建时间 ：2024-11-06  11-06-34 
//  Unity版本  ：2019.4.35f1 
//  变更时间 :  2024-11-06  11-06-34 
//===============================





using UnityEngine;

using UnityEngine.UI;
using ZS.U3D.BASE.Runtime.Event;

namespace UI.BeiBao
{

    public class TileEvent : IEvent 
    {
        public Tile tile;

        public void FireEvent() {
            EventManager.FireEvent(this);
        }
    }

    public class TileDragStartEvent : TileEvent 
    {
        
    }

    public class TileDragEndEvent : TileEvent 
    { 
        
    }

    public class Tile : MonoBehaviour
    {

        public Transform tileMoveParent;

        private Text _text;
        private MoveHandle _moveHandle;

        private Vector3 moveHandleStartPos;
        private Transform tileParent;

        

        public string Text
        {
            get
            {
                if (_text != null)
                    return _text.text;
                return string.Empty;
            }

            set
            {
                if (_text != null)
                {
                    _text.text = value;
                }
            }
        }

        [ReadOnly]
        [SerializeField]
        private Grid _grid;

        public Grid Grid {
            get {
                return _grid;
            }
            set {
                _grid = value;
            }
        }

        private CanvasGroup _canvasGroup;

        private TileDragStartEvent startEvent = new TileDragStartEvent();
        private TileDragEndEvent endEvent = new TileDragEndEvent();

        private void Awake()
        {
            _text = GetComponent<Text>();
            _moveHandle = GetComponent<MoveHandle>();
            moveHandleStartPos = _moveHandle.transform.localPosition;
            tileParent = transform.parent;

            startEvent.tile = this;
            endEvent.tile = this;

            Grid = GetComponentInParent<Grid>();
            _canvasGroup = GetComponent<CanvasGroup>();

        }

        private void OnEnable()
        {
            _moveHandle.DragStart += MoveHandle_DragStart;
            _moveHandle.DragEnd += MoveHandle_DragEnd;
        }

        private void OnDisable()
        {
            _moveHandle.DragStart -= MoveHandle_DragStart;
            _moveHandle.DragEnd -= MoveHandle_DragEnd;
        }

        private void MoveHandle_DragEnd()
        {
            _canvasGroup.blocksRaycasts = true;
            endEvent.FireEvent();
        }

        private void MoveHandle_DragStart()
        {
            _canvasGroup.blocksRaycasts = false;
            startEvent.FireEvent();
        }


        public void ReturnGrid() {
            transform.SetParent(Grid.transform);
            transform.localPosition = moveHandleStartPos;
        }

        public void ToGrid(Grid grid)
        {
            Grid.Tile = null;
            Grid = grid;
            Grid.Tile = this;
            Debug.Log(Grid.name+grid.name);
            ReturnGrid();
        }

    }
}